/**
 * 作者: wgx
 * 日期: 2025-02-17 10:43:02
 * 备注: 老虎机抽奖获得招募券记录表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameDraw.PBTigerDrawRecord;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class TigerDrawRecordModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // ID
    protected int serverId; // 服务器id
    protected long userId; // 用户ID
    protected String userName = ""; // 用户名字
    protected int itemId; // 获得的道具id
    protected long itemNum; // 获得的道具num
    protected long addTime; // 获得时间

    public void reset() {
    }

    protected String getKey() {
        return "TigerDrawRecord." + id;
    }

    protected String getListKey() {
        return "TigerDrawRecord." + serverId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("tiger_draw_record"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("serverId", serverId); // 服务器id
        obj.put("userId", userId); // 用户ID
        obj.put("userName", userName); // 用户名字
        obj.put("itemId", itemId); // 获得的道具id
        obj.put("itemNum", itemNum); // 获得的道具num
        obj.put("addTime", addTime); // 获得时间
        return obj;
    }

    public PBTigerDrawRecord.Builder takeInitBuilder() {
        PBTigerDrawRecord.Builder sb = PBTigerDrawRecord.newBuilder();
        if (userId != 0) {
            sb.setUserId(userId);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (itemId != 0) {
            sb.setItemId(itemId);
        }
        if (itemNum != 0) {
            sb.setItemNum(itemNum);
        }
        return sb;
    }

    public PBTigerDrawRecord.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBTigerDrawRecord.Builder sb = PBTigerDrawRecord.newBuilder();
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("itemId")) {
            sb.setItemId(itemId);
        }
        if (isFieldUpdate("itemNum")) {
            sb.setItemNum(itemNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public int getItemId() {
        return itemId;
    }

    public void putItemId(int itemId) {
        if (this.itemId != itemId) {
            this.itemId = itemId;
            updateRedisHashField(getKey(), "itemId", this.itemId);
        }
    }

    public void addItemId(int value) {
        this.itemId += value;
        updateRedisHashField(getKey(), "itemId", this.itemId);
    }

    public long getItemNum() {
        return itemNum;
    }

    public void putItemNum(long itemNum) {
        if (this.itemNum != itemNum) {
            this.itemNum = itemNum;
            updateRedisHashField(getKey(), "itemNum", this.itemNum);
        }
    }

    public void addItemNum(long value) {
        this.itemNum += value;
        updateRedisHashField(getKey(), "itemNum", this.itemNum);
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

}